﻿using BW.WMS.ABPDemo;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;
using Volo.Abp.EventBus.Distributed;
using System.IO;

namespace BW.WMS.LogHandler
{
    public static class LogHandler
    {
        private static readonly string LogPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Logs");
        
        static LogHandler()
        {
            if (!Directory.Exists(LogPath))
            {
                Directory.CreateDirectory(LogPath);
            }
        }

        public static void WriteLog(string message, string level = "INFO")
        {
            try
            {
                string logFile = Path.Combine(LogPath, $"WMS_{DateTime.Now:yyyy-MM-dd}.log");
                string logMessage = $"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] [{level}] {message}";
                
                File.AppendAllText(logFile, logMessage + Environment.NewLine);
                Console.WriteLine(logMessage);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"写入日志失败: {ex.Message}");
            }
        }

        public static void WriteError(string message, Exception ex = null)
        {
            string errorMessage = message;
            if (ex != null)
            {
                errorMessage += $"\n异常详情: {ex.Message}\n堆栈跟踪: {ex.StackTrace}";
            }
            WriteLog(errorMessage, "ERROR");
        }

        public static void WriteInfo(string message)
        {
            WriteLog(message, "INFO");
        }

        public static void WriteWarning(string message)
        {
            WriteLog(message, "WARNING");
        }
    }
}
